package com.wsyu.springbootbackend.core.auth.handler;

import com.wsyu.springbootbackend.core.util.HttpServletUtils;
import com.wsyu.springbootbackend.core.util.JsonBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        String json = JsonBuilder.create()
                .add("code", HttpServletResponse.SC_BAD_REQUEST)
                .add("msg", "login failure")
                .build();

        HttpServletUtils.writeJsonResponse(response, HttpStatus.BAD_REQUEST, json);
    }
}

